﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using SH.STD.Model;

namespace SH.STD.DataProvider
{
    public partial class DataAccess
    {
        public static bool IsUserNameExist(string userName)
        {
            var sql = "SELECT COUNT(*) FROM usrUser WHERE Name=@name";
            var cmdParams = new SqlParameter[] 
            {
                new SqlParameter("name",userName)
            };
            return Convert.ToInt32(dbAccess.ExecuteScalar(CommandType.Text, sql, cmdParams).ToString()) > 0;
        }

        public static UserInfo GetUserInfo(string userName, string pwd)
        {
            UserInfo ret = null;
            var sql = "SELECT * FROM usrUser WHERE Name=@name and PassWord=@pwd";
            var cmdParams = new SqlParameter[] 
            {
                new SqlParameter("name",userName),
                new SqlParameter("pwd",pwd),
            };

            using (var readerObj = dbAccess.ExecuteReader(CommandType.Text, sql, cmdParams))
            {
                while (readerObj.Read())
                {
                    ret = new UserInfo();
                    ret.UserID = readerObj["ID"].ToString().ToInt32();
                    ret.UserName = userName;
                    ret.PassWord = pwd;
                    ret.Memo = readerObj["Memo"].ToString();
                }
            }

            return ret;
        }
    }
}
